<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>7.代理模式典型应用之防抖</title>
</head>
<style>
    #container{
        width: 500px;
        height: 1000px;
        background-color: rgb(10, 219, 122);
    }
    #box{
        width: 500px;
        height: 300px;
        border: 1px solid blue;
        overflow: scroll;
    }
</style>
<body>
    <div id="box">
        <div id="container"></div>
    </div>
    <script>
        let container = document.getElementById('box');
        let lastTime = Date.now();
        function debounce(fn,interval) { console.log('111');
            let timer;
            return function () { 
                let context = this;
                if(timer){
                    console.log('222');
                    clearTimeout(timer);
                    timer = setTimeout(_=>{
                                console.log('防抖重新设置定时器');
                                fn.apply(context,arguments);
                            },interval);
                }else{
                    console.log('333');
                    timer = setTimeout(_=>{
                                console.log('防抖重新设置定时器');
                                fn.apply(context,arguments);
                            },interval);
                }
             }
         }
        function scrollFn() { 
            console.log('滚动事件触发啦～：');
         }
        container.addEventListener('scroll',debounce(scrollFn,1000),false)

    </script>
    
</body>
</html>